import logging
import os
import shutil
import re
import ctypes
from colorama import Fore,Style
import tests.update
from bin.common import *
from util import tui

utility_modules=( tests.update,  )
utility_tests = tuple(test.Create() for test in utility_modules)

def water(text):
    os.system("");
    faded = ""
    green = 10
    for line in text.splitlines():
        faded += (f"\033[38;2;0;{green};255m{line}\033[0m\n")
        if not green == 255:
            green += 15
            if green > 255:
                green = 255
    return faded

def clear_console():
    if os.name == 'nt':
        import subprocess
        subprocess.check_call('cls', shell=True, stdout=subprocess.DEVNULL)
    else:
        os.system('clear')

def align(text: str) -> str:
    """
    credits : https://github.com/SirDank/dankware/blob/main/dankware/__init__.py
    """
    width = shutil.get_terminal_size().columns
    aligned = text
    for _ in tuple(vars(Fore).values()) + tuple(vars(Style).values()):
        aligned = aligned.replace(_, '')

    text = text.splitlines()
    aligned = aligned.splitlines()

    for _ in range(len(aligned)):
        aligned[_] = aligned[_].center(width).replace(aligned[_], text[_])
    return str('\n'.join(aligned) + Style.RESET_ALL)


def draw_line():
    terminal_width = shutil.get_terminal_size().columns
    line = Fore.CYAN + "-" * terminal_width + Style.RESET_ALL
    print(line)

def panner():
    __RAW_BANNER__ = ("""
     ______
    █ = = !█
     ╚═ █  -   █ ╝  
    ^            `  `             
    """)

    clear_console()
    draw_line()
    print("Author Mr: wang")
    __BANNER__ = water(align(__RAW_BANNER__))
    print(__BANNER__)
    draw_line()
    import time
    loading_status = 0

def clear_line():
    import sys
    sys.stdout.write("\033[K")
    sys.stdout.flush()

def fade_type(text):
    for c in text:
        print(c, end='', flush=True)
        time.sleep(0.009)

# test_names = list(map(lambda t: t.name, utility_tests))
if __name__ == '__main__':
    panner()
    tui.setup_logging()
    tui.clear_console()
    # <tests.update.Update object at 0x000001976A9F0410>
    msg = loading_animation("Checking for updates",process=utility_tests[0])
    if re.search(r'false', msg, re.IGNORECASE):
        logging.error(("Checking for updates",msg))
    else:
        if msg == "":
            msg = "PASS"
        logging.info(("Checking for updates",msg))
    clear_line()
    # loading_animation("Fetching info")
    logging.info("Starting GOD")
    logging.warning("There's a new version. Are you sure you want to use this version?")
    fade_type('Answer [y/n]\n')
    if not input(">>> ").lower() == 'y':
        logging.warning("Press any key to exit...")
#        while True:
#            
#            if msvcrt.kbhit():
#                key = msvcrt.getch()
#               logging.warning("Exiting...")
#                KILL_THREAD = True
#                ctypes.windll.kernel32.ExitProcess(0)
# import msvcrt
